<?PHP
require('paymentPlugin.php');
class pay_cmbpro extends paymentPlugin{
    var $name="招商银行(专业版)";
    var $logo="bank_cmb";
    var $version=20081030;
    var $charset="utf-8";
	//var $intro = "招商银行(专业版)";
    var $submitUrl="https://netpay.cmbchina.com/netpayment/BaseHttp.dll?PrePayC2";		//正式地址3
	//var $submitUrl="https://netpay.cmbchina.com/netpayment/BaseHttp.dll?TestPrePayC2";		//测试地址3
    var $submitButton = 'http://img.alipay.com/pimg/button_alipaybutton_o_a.gif'; ##需要完善的地方
    var $supportCurrency=array("CNY"=>001);
    var $supportArea =  array("AREA_CNY");
    //var $method = "post";
    var $head_charset = "utf-8";

    function toSubmit($payment){
		$BranchID = $this->getConf($payment['M_OrderId'], 'BranchID');			//开户分行号
        $merId = $this->getConf($payment['M_OrderId'], 'member_id');			//帐号
        //$PrivateKey = $this->getConf($payment['M_OrderId'], 'PrivateKey');	//密钥
        $certFile = $this->getConf($paypment['M_OrderId'],'certFile');			//公钥文件名

		$money = number_format($payment['M_Amount'],2,'.','');		//精确到小数点后2位
		$return['BranchID'] = $BranchID;							//商户开户分行号，请咨询开户的招商银行分支机构
        $return['CoNo'] = $merId;									//商户号
		//$return['BillNo'] = '000000';								//订单号	6位或10位数字	测试时统一为'000000'
        $return['BillNo'] = substr($payment['M_OrderId'],-10);		//订单号	6位或10位数字 正式的
        $return['Amount'] = $money;									//订单金额
		$return['Date'] = date('Ymd',time());						//订单时间 格式YYYYMMDD
		//$return['Date'] = date('Ymd',($payment['M_Time']?$payment['M_Time']:time()));			//订单时间 格式YYYYMMDD
        $return['MerchantUrl'] = $this->callbackUrl;				//支付完成返回地址
		$return['MerchantPara'] = $payment['M_OrderId'];			//商户自己的参数 商店支付单号

        return $return;
    }
    function callback($in,&$paymentId,&$money,&$message,&$tradeno){
		$MerchantPara = $in['MerchantPara'];	//商户自己的参数 商店支付单号
		$paymentId = $MerchantPara;				//商店支付单号
		$Succeed = $in['Succeed'];				//订单状态 Y-成功 N-失败
        $CoNo = $in['CoNo'];					//商户号
        $BillNo = $in["BillNo"];				//订单号
		$Date = $in['Date'];					//支付时间
        $Amount = $in["Amount"];				//支付金额
		$money = $Amount;
		$Signature = $in['Signature'];			//网关签名数据
		$Msg = $in['Msg'];						//银行通知用户的支付结果消息

		$BranchID = $this->getConf($paymentId, 'BranchID');			//开户分行号
        $merId = $this->getConf($paymentId, 'member_id');			//帐号
        //$PrivateKey = $this->getConf($payment['M_OrderId'], 'PrivateKey');	//密钥
        $cfile = $this->getConf($paymentId,'certFile');			//公钥文件名

		$sign_str = 'Succeed='.$Succeed.'&CoNo='.$CoNo.'&BillNo='.$BillNo.'&Amount='.$Amount.'&Date='.$Date.'&MerchantPara='.$MerchantPara.'&Msg='.$Msg.'&Signature='.$Signature;		//签名明文

        if (file_exists(dirname(__FILE__)."/../../home/upload/cmbpro/".$cfile))
            $certFile = dirname(__FILE__)."/../../home/upload/cmbpro/".$cfile;
        elseif (file_exists(dirname(__FILE__)."/../../cert/cmbpro/".$cfile))
            $certFile = dirname(__FILE__)."/../../cert/cmbpro/".$cfile;
		else
		{
			exit('证书文件不存在!');
		}

		if (strtoupper(substr(PHP_OS,0,3)) == 'WIN')
		{
			$verify_jar = dirname(__FILE__)."/../../home/upload/cmbpro/Verify.jar";
		}
		else
		{
			$verify_jar = dirname(__FILE__)."/../../home/upload/cmbpro/Verify.jar";
		}

		$cmd = "java -jar \"{$verify_jar}\" \"{$certFile}\"  \"{$sign_str}\"";

		if ($handle = popen($cmd,'r'))
		{
			$result = fgets($handle,8);
			pclose($handle);
		}
		else
		{
			exit("无法执行签名验证!");
		}

        if (!strstr($result,'true')){
			$message = '验证失败';
            return PAY_ERROR;
        }else{
            if ($Succeed == 'Y'){
                return PAY_SUCCESS;
            }else{
				$message = '交易失败';
                return PAY_FAILED;
            }
        }
    }

    function getfields(){
        return array(
			'BranchID'=>array(
                'label'=>'开户分行号',
                'type'=>'string'
			),
            'member_id'=>array(
                'label'=>'客户号',
                'type'=>'string'
            ),
			/*
			'PrivateKey'=>array(
					'label'=>'私钥',
					'type'=>'string'
			), */
            'certFile'=>array(
                'label'=>'公钥文件',
                'type'=>'file'
			)
        );
    }
}
?>